﻿using BLL;
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using webMvc.Models.EnumModel;
using webMvc.Models.ViewModel;

namespace webMvc.Controllers
{
    public class DepartmentsController : BaseController
	{

		public ActionResult DepartmentManage()
		{
			return View();
		}

		public JsonResult treeData()
		{
			var departments = departmentService.LoadEntities(x => true).ToList();
			return Json(departments, JsonRequestBehavior.AllowGet);
		}

		IDepartmentService departmentService = new DepartmentService();
		[HttpPost]
		public byte checkDeptNameUnique(string deptName)
		{
			byte returnByte = 0;
			int count = departmentService.LoadEntities(x => x.DepName == deptName).Count();
			if (count > 0)
			{
				returnByte = 1;
			}
			return returnByte;
		}

		public JsonResult AddNewDepartment(AddDepartmentDto addDepartmentDto)
		{
			MyJsonResult myJsonResult = new MyJsonResult() { code = resultCode.Success, msg = "增加成功!" };
			try
			{
				departmentService.AddDonator(new kang.Entitys.IdentityAuthentication.Department()
				{
					IsLeaf = addDepartmentDto.IsLeaf,
					CreateTime = DateTime.Now,
					DepName = addDepartmentDto.DepName,
					ParentId = addDepartmentDto.ParentId,
					Level = "1",
					TreePath = "1"
				});
				departmentService.CurrentDbSession.allSaveChangge();
			}
			catch (Exception ex)
			{
				myJsonResult.msg = ex.Message;
				myJsonResult.code = resultCode.Fail;
			}
			return Json(myJsonResult);
		}

		public ActionResult treeDepartment()
		{
			return View();
		}

		public ActionResult AddDepartment()
		{
			return View();
		}

	}
}